<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: cyc
  Date: 2021/9/29
  Time: 11:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>易买网 - 首页</title>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/function.js"></script>
    <script src="${pageContext.request.contextPath}/scripts/jquery-1.8.3.js"></script>
</head>
<body>
<div id="header" class="wrap">
    <div id="logo"><img src="${pageContext.request.contextPath}/images/logo.gif" /></div>
    <div class="help">
        <c:if test="${sessionScope.user!=null}"><a href="shopping.jsp" id="shoppingBag" class="shopping">购物车${sessionScope.shopList.size()}件</a></c:if>
        <c:if test="${sessionScope.user==null}"><a href="login.jsp">登录</a></c:if>
        <c:if test="${sessionScope.user!=null}"> <span style="color: red">${sessionScope.user.username}你好</span> </c:if>
        <a class="button" id="logout" href="LoginOutServlet">注销</a><a href="register.jsp">注册</a>
        <a href="guestbook.jsp">留言</a>
        <a href="manage/index.html">后台管理</a>
    </div>
    <div class="navbar">
        <ul class="clearfix">
            <li class="current"><a href="${pageContext.request.contextPath}/index.jsp">首页</a></li>
            <li><a href="#">图书</a></li>
            <li><a href="#">百货</a></li>
            <li><a href="#">品牌</a></li>
            <li><a href="#">促销</a></li>
        </ul>
    </div>
</div>
<div id="childNav">
    <div class="wrap">
        <ul class="clearfix">
            <li class="first"><a href="#">音乐</a></li>
            <li><a href="#">影视</a></li>
            <li><a href="#">少儿</a></li>
            <li><a href="#">动漫</a></li>
            <li><a href="#">小说</a></li>
            <li><a href="#">外语</a></li>
            <li><a href="#">数码相机</a></li>
            <li><a href="#">笔记本</a></li>
            <li><a href="#">羽绒服</a></li>
            <li><a href="#">秋冬靴</a></li>
            <li><a href="#">运动鞋</a></li>
            <li><a href="#">美容护肤</a></li>
            <li><a href="#">家纺用品</a></li>
            <li><a href="#">婴幼奶粉</a></li>
            <li><a href="#">饰品</a></li>
            <li class="last"><a href="#">Investor Relations</a></li>
        </ul>
    </div>
</div>
<div id="position" class="wrap">
    您现在的位置：<a href="index.jsp">易买网</a> &gt; 购物车
</div>
<div class="wrap">
    <div id="shopping">
        <form id="shop_form" action="${pageContext.request.contextPath}/address/all">
            <table>
                <tr>
                    <th>商品名称</th>
                    <th>商品价格</th>
                    <th>购买数量</th>
                    <th>操作</th>
                </tr>
                <c:forEach items="${shopList}" var="i">

                    <tr id="${i.id}">
                        <td class="thumb"><img style="width: auto;height: 50px" src="${pageContext.request.contextPath}/images/product/${i.filename}" /><a href="${pageContext.request.contextPath}/product/selectbyid?id=${i.id}">${i.name}</a></td>
                        <td class="price" id="price_id_${i.id}">
                            <span>￥${i.price}</span>
                            <input type="hidden" value="99" />
                        </td>
                        <td class="number">
                            <span name="del">-</span>
                            <input number="number_id_${i.id}" type="text" name="number" value="${i.num}" />
                            <span name="add">+</span>
                        </td>
                        <td class="delete"><a ref="${i.id}" class="del" href="javascript:void(0)">删除</a></td>
                    </tr>




                </c:forEach>

            </table>
            <input type="text" name="cost" value="" style="display: none">
            <div class="total"><span id="total">总计：￥0</span></div>
            <div class="button"><input type="submit" value="" /></div>
        </form>
    </div>

</div>
<div id="footer">
    Copyright &copy; 2013 北大青鸟 All Rights Reserved. 京ICP证1000001号
</div>
<script src="${pageContext.request.contextPath}/scripts/jquery-1.8.3.js"></script>
<script>
    $(function (){

        $(".del").click(function (){
            var id=$(this).attr("ref");
            var data={"id":id};
            $.post("${pageContext.request.contextPath}/cart/del",data,function (res){
                alert(res.msg);
                location.href="${pageContext.request.contextPath}/cart/get";
            },"json")
        });


        function pricrToyal(){
            var sum=0;
            <c:forEach items="${shopList}" var="i">
            var p=${i.price};
            var number=$("input[number=number_id_${i.id}]").val();
            sum=parseFloat(p)*parseInt(number)+sum;
            </c:forEach>
            $("#total").text("总计：￥"+sum);
            $("input[name=cost]").val(sum);

        }


        pricrToyal();

        $("span[name=add]").click(function (){
            var p=Number(parseInt($(this).prev().val())+1);
            $(this).prev().val(p);
            pricrToyal();

        });
        $("span[name=del]").click(function (){
            var p=Number(parseInt($(this).next().val())-1);
            if (p<1){
                alert("不能再减少啦")
            }else {
                $(this).next().val(p);
            }
            pricrToyal();

        });





    });
</script>
</body>
</html>
